<?php


namespace FoxLib\Services;

use GuzzleHttp\Client;

class DebugService
{
    public static function trace($uri, $method, $count, $time, $sql)
    {
        try {
            $apiTraceConfigFile =  base_path() . '/apitrace.json';
            if (file_exists($apiTraceConfigFile)) {
                $apiTraceConfig = json_decode(file_get_contents($apiTraceConfigFile), true);
                $client = new Client();
                $response = $client->post($apiTraceConfig['host'] . '/api/sync/project',
                    [
                        'json' => [
                            'name' => $apiTraceConfig['project'],
                            'description' => $apiTraceConfig['description']
                        ]
                    ]);
                $result = json_decode($response->getBody(), true);
                if (!empty($result['projectId'])) {
                    $response = $client->post($apiTraceConfig['host'] . '/api/sync/sql',
                        [
                            'json' => [
                                'projectId' => $result['projectId'],
                                'sql' => [
                                    'url' => $uri,
                                    'count' => $count,
                                    'method' => $method,
                                    'sql' => $sql
                                ]
                            ]
                        ]);
                    $result = json_decode($response->getBody(), true);
                }
            } else {
                var_dump('apitrace.json文件不存在');
            }
        } catch (\Exception $e) {
            var_dump($e->getMessage());
        }
    }
}